#include <vector>
#include <set>
#include <iostream>
#include <unordered_map>
#include <numeric>

using namespace std;

// 超时
int mymaxRotateFunction(vector<int>& nums) {
    int max = INT_MIN;
    for(int z = 0; z < nums.size(); z++){
        int sum = 0;
        for(int i = 0; i < nums.size(); i++){
            sum += i * nums[(i+z)%nums.size()];
        }
        if(sum > max){ max = sum; }
    }
    return max;
}

int maxRotateFunction(vector<int>& num){
    int f = 0, n = num.size();
    int numSum = accumulate(num.begin(), num.end(), 0);
    for (int i = 0; i < n; i++) {
        f += i * num[i];
    }
    int res = f;
    for (int i = n - 1; i > 0; i--) {
        f += numSum - n * num[i];
        res = max(res, f);
    }
    return res;
}

int main(){
    vector<int> nums = { 4,3,2,6 };
    cout << maxRotateFunction(nums) << endl;
    return 0;
}